package com.cdut.beans;


import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;
import utils.Digests;
import utils.Exceptions;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

public class PasswordKit {
    public final static int SALT_COUNT=8;
    public final static int SALT_STR_LENGTH=16;
    public final static int HASH_ITERATIONS=1024;
    public final static String ENCODE_CHARSET="UTF-8";

    public static byte[] parseSalt(String encodePassword){
        String saltStr=encodePassword.substring(0, SALT_STR_LENGTH);
        byte[] salt = null;
        try {
            salt = Hex.decodeHex(saltStr.toCharArray());
        } catch (DecoderException e) {
            Exceptions.unchecked(e);
        }
        return salt;
    }
    public static String parseHashPassword(String encodePassword){
        String hashPassword=encodePassword.substring(SALT_STR_LENGTH);
        return hashPassword;
    }
    /**
     * md5加密输出十六进制的大写字母密文
     * @param password
     * @return
     */
    public static String encodeMd5(String password){
        byte[] out = null;
        try {
            out = Digests.md5(new ByteArrayInputStream(password.getBytes(ENCODE_CHARSET)));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if(null==out) {
            return null;
        }
        return Hex.encodeHexString(out).toUpperCase();
    }
    public static String encode(String password){
        byte[] salt=Digests.generateSalt(SALT_COUNT);
        byte[] input=null;
        try {
            input=password.getBytes(ENCODE_CHARSET);
        } catch (UnsupportedEncodingException e) {
            Exceptions.unchecked(e);
        }
        byte[] out=Digests.sha1(input, salt,HASH_ITERATIONS);
        String saltStr=Hex.encodeHexString(salt);
        String hashPassword=Hex.encodeHexString(out);
        String encodePassord = saltStr +hashPassword;
        return encodePassord;
    }
    public static boolean valid(String encodePassword,String plainPassword){
        byte[] salt=parseSalt(encodePassword);
        byte[] input=null;
        try {
            input=plainPassword.getBytes(ENCODE_CHARSET);
        } catch (UnsupportedEncodingException e) {
            Exceptions.unchecked(e);
        }
        byte[] plainPasswordBytes=Digests.sha1(input, salt,HASH_ITERATIONS);
        String saltStr=Hex.encodeHexString(salt);
        String hashPassword=Hex.encodeHexString(plainPasswordBytes);
        String inputPassword=saltStr+hashPassword;
        return inputPassword.equals(encodePassword);
    }
    public static void main(String[] args){
        System.out.println("Test Password");

		/*
		String password="123";
		byte[] salt = DigestKit.generateSalt(8); // 8*8=64bit
		String saltStr = EncodeKit.encodeHex(salt); // 16 char
		byte[] hashPassword = DigestKit.sha1(password.getBytes(), salt, 1024);// 160bit
		String ep = EncodeKit.encodeHex(hashPassword); // 40 char
		String savePassword = saltStr + ep; // 56 char
		System.out.println("Salt:"+saltStr);
		System.out.println("Digest Password:"+ep);
		System.out.println("Save Password:"+savePassword);
		//System.out.println(valid(savePassword,"123"));
		Sha1Hash sha1Hash =new Sha1Hash(password, salt, 1024);
		System.out.println("Salt:"+sha1Hash.getSalt().toHex());
		System.out.println("Hash1:"+sha1Hash.toHex());*/

		/*String ep=encode("123");
		System.out.println("encodePassword:"+ep);
		System.out.println("encodePassword length:"+ep.length());
		System.out.println(valid(ep, "123"));*/
		/*
		String encode;
		System.out.println(encode=encodeMd5("htx321abc"));
		System.out.println("EA2FCBA4C402F0627B1669760CCF34D7".equals(encode));
		*/
        System.out.println(encode("123"));
    }
}




